using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace hao_Dto.User
{
    public class UserDto
    {
        public Guid Id { get; set; }
        public string UserName { get; set; }
        public string? Nickname { get; set; }
        public string Password { get; set; }        // 密码（加密存储）
        public string Avatar { get; set; }
        // 移除这两个字段
        // public int Level { get; set; }
        // public string LevelName { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public int? Gender { get; set; }
        public DateTime? Birthday { get; set; }
        public DateTime RegisterTime { get; set; }
        public DateTime? LastLoginTime { get; set; }
        public int Status { get; set; }
    }

    public class RoleDto
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }

    public class UserRoleDto
    {
        public Guid Id { get; set; }
        public Guid UserId { get; set; }
        public Guid RoleId { get; set; }
        // 可选：角色名称，便于前端展示
        public string RoleName { get; set; }
    }

    public class UserWithRolesDto
    {
        public Guid Id { get; set; }
        public string UserName { get; set; }
        public string? Nickname { get; set; }
        public List<RoleDto> Roles { get; set; }
    }

    public class LoginDto
    {
        public string UserName { get; set; }        // 用户名或手机号或邮箱
        public string Password { get; set; }        // 密码
    }

    public class LoginResponseDto
    {
        public string Token { get; set; }           // JWT Token
        public DateTime ExpireTime { get; set; }    // 过期时间
        public UserDto User { get; set; }           // 用户信息
        public List<RoleDto> Roles { get; set; }    // 用户角色
    }

    public class RegisterDto
    {
        [Required(ErrorMessage = "用户名不能为空")]
        [StringLength(50, ErrorMessage = "用户名长度不能超过50个字符")]
        public string UserName { get; set; }
        [StringLength(50, ErrorMessage = "昵称长度不能超过50个字符")]
        public string? Nickname { get; set; }
        
        [Required(ErrorMessage = "密码不能为空")]
        [StringLength(100, MinimumLength = 6, ErrorMessage = "密码长度必须在6-100个字符之间")]
        public string Password { get; set; }
        
        [Phone(ErrorMessage = "手机号格式不正确")]
        public string Phone { get; set; }
        
        [Required(ErrorMessage = "邮箱不能为空")]
        [EmailAddress(ErrorMessage = "邮箱格式不正确")]
        public string Email { get; set; }
    }

    /// <summary>
    /// 用户信息更新数据传输对象
    /// </summary>
    public class UpdateUserDto
    {
        public string UserName { get; set; }        // 用户名
        public string? Nickname { get; set; }       // 昵称 (可选)
        public string? Avatar { get; set; }         // 头像 (可选)
        public string Phone { get; set; }           // 手机号
        public string? Email { get; set; }          // 邮箱 (可选)
        public int? Gender { get; set; }            // 性别：0-未知，1-男，2-女
        public DateTime? Birthday { get; set; }     // 生日
    }

    /// <summary>
    /// 密码修改数据传输对象.
    /// </summary>
    public class ChangePasswordDto
    {
        public string OldPassword { get; set; }     // 原密码
        public string NewPassword { get; set; }     // 新密码
        public string ConfirmPassword { get; set; } // 确认新密码
    }

    /// <summary>
    /// 刷新令牌数据传输对象
    /// </summary>
    public class RefreshTokenDto
    {
        public string Token { get; set; }           // 当前Token
        public string RefreshToken { get; set; }    // 刷新Token
    }

    /// <summary>
    /// 用户状态枚举
    /// </summary>
    public enum UserStatus
    {
        /// <summary>
        /// 正常
        /// </summary>
        Normal = 1,
        /// <summary>
        /// 禁用
        /// </summary>
        Disabled = 2,
        /// <summary>
        /// 锁定
        /// </summary>
        Locked = 3,
        /// <summary>
        /// 删除
        /// </summary>
        Deleted = 4
    }

    /// <summary>
    /// 用户状态更新数据传输对象
    /// </summary>
    public class UpdateUserStatusDto
    {
        public int Status { get; set; }         // 用户状态
        public string Reason { get; set; }      // 状态变更原因
    }

    /// <summary>
    /// 批量操作数据传输对象
    /// </summary>
    public class BatchOperationDto
    {
        public List<Guid> UserIds { get; set; }  // 用户ID列表
        public UserStatus Status { get; set; }  // 操作状态
        public string Reason { get; set; }      // 操作原因
    }

    /// <summary>
    /// 用户搜索数据传输对象
    /// </summary>
    public class UserSearchDto
    {
        public string Keyword { get; set; }     // 搜索关键词（用户名、邮箱、手机号）
        public UserStatus? Status { get; set; } // 用户状态筛选
        public int? Gender { get; set; }        // 性别筛选
        public int? Level { get; set; }         // 等级筛选
        public DateTime? StartDate { get; set; } // 注册开始时间
        public DateTime? EndDate { get; set; }   // 注册结束时间
        public int PageNumber { get; set; } = 1; // 页码
        public int PageSize { get; set; } = 10;  // 页大小
        public string SortBy { get; set; }      // 排序字段
        public bool SortDesc { get; set; }      // 是否降序
    }

    /// <summary>
    /// 角色分配数据传输对象
    /// </summary>
    public class AssignRolesDto
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }
        
        /// <summary>
        /// 角色ID列表
        /// </summary>
        public List<Guid> RoleIds { get; set; } = new List<Guid>();
    }

    /// <summary>
    /// 管理员重置用户密码数据传输对象
    /// </summary>
    public class ResetUserPasswordDto
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }
        
        /// <summary>
        /// 新密码
        /// </summary>
        [Required(ErrorMessage = "新密码不能为空")]
        [StringLength(100, MinimumLength = 6, ErrorMessage = "密码长度必须在6-100个字符之间")]
        public string NewPassword { get; set; }
    }
}